package hk.ust.android_competition.view_adapter;

import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import hk.ust.android_competition.activities.MainActivity;
import hk.ust.android_competition.data_reload_tools.CostumeListViewAdapter;
import hk.ust.android_competition.data_reload_tools.DefaultListViewAdapter;
import hk.ust.android_competition.e_pet.R;
import hk.ust.android_competition.pet.PetStatus;
import hk.ust.android_competition.store.Costume;
import hk.ust.android_competition.user.UserData;

public class MainViewAdapter extends ViewAdapter {
    private Button yes_button;
    private Button no_button;
    private TextView question_in_message_box;
    private Button showStatus;
    private Button timePass;
    private Button reset;
    private Button openStore;
    private Button openEvent;
    private TextView showUserName;
    private TextView showCoins;
    private TextView showPetName;
    private TextView showHappiness;
    private TextView showHealth;
    private TextView showSleepy;
    private TextView showClean;
    private TextView message;
    private ImageView iv_message;
    private ProgressBar happiness_progress;
    private ProgressBar health_progress;
    private ProgressBar sleepy_progress;
    private ProgressBar clean_progress;
    private ImageView pet_image;
    private ListView lv_store;
    private ListView lv_event;
    private ListView lv_store_lv;
    private ListView lv_event_lv;
    private final int[] icons_in_store = {R.drawable.food_icon, R.drawable.costume_icon, R.drawable.clean_icon};
    private final int[] icons_in_event = {R.drawable.talk_icon, R.drawable.game_icon, R.drawable.sing_icon};
    private final int[] icon_food = {R.drawable.apple, R.drawable.banana, R.drawable.pear, R.drawable.cake, R.drawable.burger, R.drawable.hotpot, R.drawable.medicine};
    private final int[] icon_costume = {R.drawable.pilot, R.drawable.student};
    private final int[] icon_clean = {R.drawable.shower_icon};
    private final int[] icon_talk = {R.drawable.talk_icon, R.drawable.talk_icon, R.drawable.talk_icon, R.drawable.talk_icon};
    private final int[] icon_sing = {R.drawable.sing_icon, R.drawable.sing_icon};


    private ArrayList<ListView> listViews = new ArrayList<ListView>();
    private PetStatus pet_status = PetStatus.Normal;
    private UserData ud;

    private int feelingCounter = 0;
    private int isSpeaking = 0;
    private ImageView costume_image;
    private RelativeLayout yes_no_message_box_frame;

    public MainViewAdapter(MainActivity activity, int contentViewId, UserData ud) {
        super(activity, contentViewId);
        this.ud = ud;
        initialize();
    }

    public void setPet_status(PetStatus pet_status) {
        if (this.pet_status == PetStatus.Normal) {
            switch (pet_status) {
                case Sleep:
                    feelingCounter = 10;
                    break;
                case Smile:
                    feelingCounter = 10;
                    break;
            }
            this.pet_status = pet_status;
        } else if (this.pet_status == PetStatus.Sleep) {
            showMessageBubble("I am sleeping!!");
        }

    }

    //This update method is synchronous.
    public void update(UserData ud) {
        this.ud = ud;
        updatePetImage();
        updateStatusBar();
        if (isSpeaking != 0) {
            isSpeaking--;
            if (isSpeaking == 0)
                hideMessageBubble();
        }
    }

    public void updateAll(UserData ud) {
        update(ud);
        updateCostume(ud);
    }

    //This update method is asynchronous.
    public void showMessageBubble(String s) {
        resetAllListView();
        openEvent.setVisibility(View.VISIBLE);
        message.setVisibility(View.VISIBLE);
        message.setText(s);
        iv_message.setVisibility(View.VISIBLE);
        isSpeaking = 30;
    }

    //This update method is asynchronous.
    public void petImageIsTouched() {
        hideMessageBox();
        boolean listIsShown = false;
        for (int i = 0; i < listViews.size() && !listIsShown; i++) {
            if (listViews.get(i).getVisibility() == View.VISIBLE)
                listIsShown = true;
        }
        if (listIsShown) {
            resetAllListView();
        } else if (pet_status == PetStatus.Normal) {
            setPet_status(PetStatus.Smile);
        }
    }

    //This update method is asynchronous.
    public void sideButtonIsTouched(int sideButtonId) {
        hideMessageBubble();
        switch (sideButtonId) {
            case R.id.open_store_button:
                openStore.setVisibility(View.GONE);
                setDefaultListItem(R.array.store_items, lv_store, icons_in_store);
                break;
            case R.id.open_event_button:
                openEvent.setVisibility(View.GONE);
                setDefaultListItem(R.array.event_items, lv_event, icons_in_event);
                break;
        }
    }

    //This update method is asynchronous.
    public void updateListView(int viewId, int position) {
        switch (viewId) {
            case R.id.lv_store:
                switch (position) {
                    case 0: //food
                        setDefaultListItem(R.array.fruit_store, lv_store_lv, icon_food);
                        break;
                    case 1: //costume
                        setCostumeListItem(R.array.costume_store, lv_store_lv, icon_costume, ud.getCostumeCategory().getCostumeAvailable());
                        break;
                    case 2: //clean
                        setDefaultListItem(R.array.clean_store, lv_store_lv, icon_clean);
                        break;
                }
                break;
            case R.id.lv_event:
                switch (position) {
                    case 0://talk
                        setDefaultListItem(R.array.talk, lv_event_lv, icon_talk);
                        break;
                    case 2://sing
                        setDefaultListItem(R.array.sing, lv_event_lv, icon_sing);
                        break;
                }
                break;
        }

    }

    private void initialize() {
        yes_button = (Button) getActivity().findViewById(R.id.button_yes);
        yes_button.setOnClickListener(getActivity());
        no_button = (Button) getActivity().findViewById(R.id.button_no);
        no_button.setOnClickListener(getActivity());
        yes_no_message_box_frame = (RelativeLayout) getActivity().findViewById(R.id.yes_no_frame);
        question_in_message_box = (TextView) getActivity().findViewById(R.id.tv_message_display);
        happiness_progress = (ProgressBar) getActivity().findViewById(R.id.happiness_progress);
        health_progress = (ProgressBar) getActivity().findViewById(R.id.health_progress);
        sleepy_progress = (ProgressBar) getActivity().findViewById(R.id.sleepy_progress);
        clean_progress = (ProgressBar) getActivity().findViewById(R.id.clean_progress);
        showUserName = (TextView) getActivity().findViewById(R.id.user_name);
        showCoins = (TextView) getActivity().findViewById(R.id.coins);
        showPetName = (TextView) getActivity().findViewById(R.id.pet_name);
        showHappiness = (TextView) getActivity().findViewById(R.id.happiness);
        showHealth = (TextView) getActivity().findViewById(R.id.health);
        showSleepy = (TextView) getActivity().findViewById(R.id.sleepy);
        showClean = (TextView) getActivity().findViewById(R.id.clean);
        message = (TextView) getActivity().findViewById(R.id.message);
        iv_message = (ImageView) getActivity().findViewById(R.id.iv_messagebox);
        showStatus = (Button) getActivity().findViewById(R.id.show_status);
        showStatus.setOnClickListener(getActivity());
        timePass = (Button) getActivity().findViewById(R.id.time_pass);
        timePass.setOnClickListener(getActivity());
        reset = (Button) getActivity().findViewById(R.id.reset);
        reset.setOnClickListener(getActivity());
        pet_image = (ImageView) getActivity().findViewById(R.id.iv_pet_main);
        pet_image.setOnClickListener(getActivity());
        costume_image = (ImageView) getActivity().findViewById(R.id.iv_costume_layer);
        openStore = (Button) getActivity().findViewById(R.id.open_store_button);
        openStore.setOnClickListener(getActivity());
        openEvent = (Button) getActivity().findViewById(R.id.open_event_button);
        openEvent.setOnClickListener(getActivity());
        lv_store = (ListView) getActivity().findViewById(R.id.lv_store);
        listViews.add(lv_store);
        lv_event = (ListView) getActivity().findViewById(R.id.lv_event);
        listViews.add(lv_event);
        lv_store_lv = (ListView) getActivity().findViewById(R.id.lv_store_lv);
        listViews.add(lv_store_lv);
        lv_event_lv = (ListView) getActivity().findViewById(R.id.lv_event_lv);
        listViews.add(lv_event_lv);
        showUserName.setText("User name: " + ud.getName());
        showCoins.setText("Coins: " + ud.getCoins());
        showPetName.setText("Pet name: " + ud.getPet().getPetName());
    }

    private int[] costume_image_id = {R.drawable.pilot, R.drawable.student};

    //This is asynchronous
    public void updateCostume(UserData ud) {
        this.ud = ud;
        Costume c = ud.getPet().getCostume();
        int costumeId = c.getId();
        if (costumeId >= 0) {
            costume_image.setImageResource(costume_image_id[costumeId]);
            costume_image.setVisibility(View.VISIBLE);
        } else
            costume_image.setVisibility(View.GONE);
    }

    private void hideMessageBubble() {
        message.setVisibility(View.GONE);
        iv_message.setVisibility(View.GONE);
    }

    private void updatePetImage() {
        pet_image.setVisibility(View.VISIBLE);
        if (feelingCounter != 0)
            feelingCounter--;
        else
            pet_status = PetStatus.Normal;

        if (pet_status == PetStatus.Normal)
            if (!ud.getPet().isClean())
                pet_status = PetStatus.Dirty;
            else if (!ud.getPet().isHealthy())
                pet_status = PetStatus.Sick;
            else if (!ud.getPet().isHappy())
                pet_status = PetStatus.Cry;
            else if (ud.getPet().isSleepy())
                pet_status = PetStatus.Sleep;

        switch (pet_status) {
            case Normal:
                pet_image.setImageResource(pet_image_source[ud.getPet().getType().getValue()][0]);
                break;
            case Smile:
                pet_image.setImageResource(pet_image_source[ud.getPet().getType().getValue()][1]);
                break;
            case Sleep:
                pet_image.setImageResource(pet_image_source[ud.getPet().getType().getValue()][2]);
                break;
            case Sick:
                pet_image.setImageResource(pet_image_source[ud.getPet().getType().getValue()][3]);
                break;
            case Cry:
                pet_image.setImageResource(pet_image_source[ud.getPet().getType().getValue()][4]);
                break;
            case Dirty:
                pet_image.setImageResource(pet_image_source[ud.getPet().getType().getValue()][5]);
                break;
            default:
                pet_image.setImageResource(pet_image_source[ud.getPet().getType().getValue()][0]);
                break;
        }
    }

    private void hideAllListView() {
        for (int i = 0; i < listViews.size(); i++) {
            listViews.get(i).setVisibility(View.GONE);
        }
    }

    public void resetAllListView() {
        openStore.setVisibility(View.VISIBLE);
        openEvent.setVisibility(View.VISIBLE);
        hideAllListView();
    }

    private void hideAll() {
        hideAllListView();
        hideMessageBox();
    }

    private void setDefaultListItem(int itemArrayId, ListView listView, int[] icons) {
        hideAllListView();
        listView.setVisibility(View.VISIBLE);
        String[] itemStrings = getRes().getStringArray(itemArrayId);
        DefaultListViewAdapter adapter = new DefaultListViewAdapter(getActivity(), itemStrings, icons);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(getActivity());
        listView.setFastScrollEnabled(true);
    }

    private void setCostumeListItem(int itemArrayId, ListView listView, int[] icons, Boolean[] costumeAvailableList) {
        hideAll();
        listView.setVisibility(View.VISIBLE);
        String[] itemStrings = getRes().getStringArray(itemArrayId);
        CostumeListViewAdapter adapter = new CostumeListViewAdapter(getActivity(), itemStrings, icons, costumeAvailableList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(getActivity());
        listView.setFastScrollEnabled(true);
    }

    private void updateStatusBar() {
        int currentHappiness = ud.getPet().getHappiness();
        int maxHappiness = ud.getPet().maxHappiness + ud.getPet().getCostume().getMaxStatusIncrease();
        showHappiness.setText("Happiness: " + currentHappiness + "/" + maxHappiness);
        happiness_progress.setMax(maxHappiness);
        happiness_progress.setProgress(currentHappiness);

        int currentHealth = ud.getPet().getHealth();
        int maxHealth = ud.getPet().maxHealth + ud.getPet().getCostume().getMaxStatusIncrease();
        showHealth.setText("Health: " + currentHealth + "/" + maxHealth);
        health_progress.setMax(maxHealth);
        health_progress.setProgress(currentHealth);

        int currentSleepy = ud.getPet().getSleepy();
        int maxSleepy = ud.getPet().maxSleepy + ud.getPet().getCostume().getMaxStatusIncrease();
        showSleepy.setText("Sleepy: " + currentSleepy + "/" + maxSleepy);
        sleepy_progress.setMax(maxSleepy);
        sleepy_progress.setProgress(currentSleepy);

        int currentClean = ud.getPet().getClean();
        int maxClean = ud.getPet().maxClean + ud.getPet().getCostume().getMaxStatusIncrease();
        showClean.setText("Clean: " + currentClean + "/" + maxClean);
        clean_progress.setMax(maxClean);
        clean_progress.setProgress(currentClean);

        showCoins.setText("Coins: " + ud.getCoins());

        if (!ud.getPet().isClean())
            changeTextColor(showClean, Color.RED);
        else
            changeTextColor(showClean, Color.GREEN);

        if (!ud.getPet().isHealthy())
            changeTextColor(showHealth, Color.RED);
        else
            changeTextColor(showHealth, Color.GREEN);

        if (!ud.getPet().isHappy())
            changeTextColor(showHappiness, Color.RED);
        else
            changeTextColor(showHappiness, Color.GREEN);

        if (ud.getPet().isSleepy())
            changeTextColor(showSleepy, Color.RED);
        else
            changeTextColor(showSleepy, Color.GREEN);
    }

    private void changeTextColor(TextView show, int color) {
        show.setTextColor(color);
    }

    public void showMessageBox(String message) {
        question_in_message_box.setText(message);
        yes_no_message_box_frame.setVisibility(View.VISIBLE);
        yes_button.setText("Yes");
        yes_button.setClickable(true);
    }

    public void hideMessageBox() {
        yes_no_message_box_frame.setVisibility(View.GONE);
    }
}
